package com.example.personalmemo.slice;

import com.example.personalmemo.ResourceTable;
import com.example.personalmemo.Util.GetTime;
import com.example.personalmemo.dataName.DataAbilityName;
import com.example.personalmemo.recordInfo.MyItem;
import com.example.personalmemo.recordInfo.RecordInfo;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;


public class RewritesSlice extends AbilitySlice {

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "Demo");

    private DataAbilityHelper dataAbilityHelper;
    private DataAbilityPredicates predicates;

    private static final String FOR_MAT = "yyyy/MM/dd hh:mm:ss";
    private static final String COLUMN_TITLE = "title";
    private static final String COLUMN_TEXT = "textinfo";
    private static final String COLUMN_TIME= "time";


    private TextField mTitleText;
    private TextField mTextFeild;
    private String mTitle1;
    private String mText1;
    private Integer mIndex;

    @Override
    protected void onStart(Intent intent) {
        HiLog.info(LABEL_LOG, "RewritesSlice onStart");

        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_rewrite);

        dataAbilityHelper = DataAbilityHelper.creator(getContext());

        if (intent.getStringParam("titlekey") != null && intent.getStringParam("textkey") != null) {
            mTitle1 = intent.getStringParam("titlekey");
            mText1 = intent.getStringParam("textkey");

            // 更新文件 默认索引值
            Integer defaultValue = 1000;
            mIndex = intent.getIntParam("indexkey", defaultValue);

            initCompent();

            HiLog.info(LABEL_LOG, "mTitleText:" + mTitleText + " mTextFeild:" + mTextFeild + " mIndex:" + mIndex);
        }

    }

    private void initCompent() {

        HiLog.info(LABEL_LOG, "RewritesSlice initCompent");

        mTitleText = findComponentById(ResourceTable.Id_title_textfeild_rewrite);
        mTextFeild = findComponentById(ResourceTable.Id_good_record_textfiled_rewrite);

        mTitleText.setText(mTitle1);
        mTextFeild.setText(mText1);

        Button upDataBtn = findComponentById(ResourceTable.Id_sava_btn_rewrite);
        upDataBtn.setClickedListener(com -> upDatas());


    }

    private void upDatas() {

        // 获取标题信息
        String titleText = mTitleText.getText();
        // 获取文本域信息
        String text = mTextFeild.getText();
        // 获取系统当前时间
        String time = GetTime.getTime(FOR_MAT);

        if (text == null && titleText == null) {
            new ToastDialog(getContext()).setText("未记录消息").show();
            HiLog.info(LABEL_LOG, "未记录消息");

        }
        ValuesBucket bucket = new ValuesBucket();

        bucket.putString(COLUMN_TITLE, titleText);
        bucket.putString(COLUMN_TEXT, text);
        bucket.putString(COLUMN_TIME, time);

        predicates = new DataAbilityPredicates();
        predicates.equalTo("id", mIndex);

        try {
            // 更新数据
            int insert = dataAbilityHelper.update(Uri.parse(DataAbilityName.DATA_URI + "/infodata/update"), bucket, predicates);

            if (insert != -1) {
                new ToastDialog(getContext()).setText("更新成功").show();

                // 清理
                Cleans();
                present(new MainAbilitySlice(), new Intent());
            } else {
                new ToastDialog(getContext()).setText("更新失败，请联系管理员").show();
            }

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }


    }


    // 清理上次记录
    private void Cleans() {
        HiLog.info(LABEL_LOG, "informationPageSlice Cleans");
        mTitleText.setText(null);
        mTextFeild.setText(null);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
